package com.example.birdfriends.utils.post;

import com.example.birdfriends.pojo.post.Post;
import com.example.birdfriends.pojo.post.PostFront;
import com.example.birdfriends.pojo.post.PostsAndAuthors;
import com.example.birdfriends.pojo.user.User;
import com.example.birdfriends.service.PostService;
import com.example.birdfriends.service.UserService;
import com.example.birdfriends.utils.convert.ConvertService;
import com.example.birdfriends.utils.sort.Sort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PostUtil {
    @Autowired
    private PostService postService;
    @Autowired
    private ConvertService convertService;
    @Autowired
    private Sort sort;
    @Autowired
    private UserService userService;

    public PostsAndAuthors findMyFollowUsersAndPosts(List<Long> followedUids, Long curLoginUid){
        // 通过被关注的用户id查找这些用户的帖子
        List<Post> postList = new ArrayList<>();
        for(Long followedUid: followedUids){
            // 被关注的用户id
            List<Post> posts = postService.findPostsByUid(followedUid);  // 当前关注者所发布的贴子，此时按照发布时间排序
            // 将帖子这个加入到最终的列表
            postList.addAll(posts);
        }
        postList = sort.sortByCreateAt(postList);
        return  convertService.findPostsAuthors(postList, curLoginUid, "main");
    }

}
